package com.kensure.shike.order.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;

import com.kensure.shike.order.model.SKFinPay;
import com.kensure.shike.user.service.SKUserFRService;
import com.kensure.shike.weixinpay.model.WXPayinfo;

/**
 * 订单异步操作 服务实现类
 * 
 * @author fankd created on 2019-7-6
 * @since
 */
@Service
public class SKFinPayASyncService extends JSBaseService {
	@Resource
	private SKFinPayService sKFinPayService;
	@Resource
	private SKUserFRService sKUserFRService;

	/**
	 * 异步线程调用
	 */
	public void asyncAfterFk() {
		new Thread(() -> asyncAll()).start();
	}

	public void asyncAll() {
		// 设置收款后的状态
		sKFinPayService.fkop();
		// 入账
		sKUserFRService.commitZhang();
	}

	/**
	 * 付款成功后，进行的操作
	 */
	public synchronized void fkop() {
		Map<String, Object> parameters = MapUtils.genMap("startStatus", 1, "fkop", 0, "orderby", "id");
		List<SKFinPay> list = sKFinPayService.selectByWhere(parameters);
		if (CollectionUtils.isEmpty(list)) {
			return;
		}
		for (SKFinPay finpay : list) {
			sKFinPayService.fkop(finpay);
		}
	}

	/**
	 * 用户微信支付订单成功，返回支付订，同时入账
	 */
	public synchronized void successPay(WXPayinfo info) {
		sKFinPayService.weiXinPay(info);
		// 付款后操作，异步
		asyncAfterFk();
	}
	
	/**
	 * 用户余额支付订单成功，返回支付订，同时入账
	 */
	public synchronized void yuePay(Long id, Double money, Integer type) {
		sKFinPayService.payYue(id, money, type);
		// 付款后操作，异步
		asyncAfterFk();
	}
}
